
/**
 * settingsdialog.cpp
 * 
 * SettingsDialog is the Settings dialog
 * 
 * Copyright (c) 2008 Harunobu Oyama <nobukichi at gmail.com>
 * Licensed under GNU GPL version 3
 *
 */

#include	<QFileDialog>
#include	"usersettings.h"
#include	"settingsdialog.h"

SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags flags)
: QDialog(parent, flags|Qt::WindowSystemMenuHint)
{
	setupUi(this);

	lineEditUserName->setText(UserSettings::get()->twitterUserName());
	lineEditPassword->setText(UserSettings::get()->twitterPassword());
	checkBoxSavePassword->setChecked(UserSettings::get()->saveTwitterPassword());
	
	lineEditGatewayUserName->setText(UserSettings::get()->gatewayUserName());
	lineEditEncryptionKey->setText(UserSettings::get()->encryptionKey());
	checkBoxSaveEncryptionKey->setChecked(UserSettings::get()->saveEncryptionKey());

	spinBoxUpdateInterval->setValue(UserSettings::get()->autoUpdateInterval());
	checkBoxSaveLog->setChecked(UserSettings::get()->saveLog());
	lineEditLogPath->setText(UserSettings::get()->logPath());
	lineEditLogPath->setEnabled(checkBoxSaveLog->isChecked());
	buttonBrowseLogFolders->setEnabled(checkBoxSaveLog->isChecked());

	checkBoxUseHttpProxy->setChecked(UserSettings::get()->useHttpProxy());
	lineEditHttpProxy->setText(UserSettings::get()->httpProxy());
	lineEditPort->setText(QString::number(UserSettings::get()->httpProxyPort()));
	
	checkBoxProxyAuthentication->setChecked(!UserSettings::get()->proxyUserName().isEmpty());
	lineEditProxyUserName->setText(UserSettings::get()->proxyUserName());
	lineEditProxyPassword->setText(UserSettings::get()->proxyPassword());
	checkBoxSaveProxyPassword->setChecked(UserSettings::get()->saveProxyPassword());

	slotUpdateHttpProxySettingsWidgets();

	connect(checkBoxUseHttpProxy, SIGNAL(toggled(bool)), this, SLOT(slotUpdateHttpProxySettingsWidgets()));
	connect(checkBoxProxyAuthentication, SIGNAL(toggled(bool)), this, SLOT(slotUpdateHttpProxySettingsWidgets()));
	connect(checkBoxSaveLog, SIGNAL(toggled(bool)), lineEditLogPath, SLOT(setEnabled(bool)));
	connect(checkBoxSaveLog, SIGNAL(toggled(bool)), buttonBrowseLogFolders, SLOT(setEnabled(bool)));
	connect(buttonBrowseLogFolders, SIGNAL(clicked()), this, SLOT(slotBrowseLogPath()));
}

void SettingsDialog::accept()
{
	UserSettings::get()->setTwitterUserName(lineEditUserName->text());
	UserSettings::get()->setTwitterPassword(lineEditPassword->text());
	UserSettings::get()->setSaveTwitterPassword(checkBoxSavePassword->isChecked());

	UserSettings::get()->setGatewayUserName(lineEditGatewayUserName->text());
	UserSettings::get()->setEncryptionKey(lineEditEncryptionKey->text());
	UserSettings::get()->setSaveEncryptionKey(checkBoxSaveEncryptionKey->isChecked());

	UserSettings::get()->setAutoUpdateInterval(spinBoxUpdateInterval->value());
	UserSettings::get()->setSaveLog(checkBoxSaveLog->isChecked());
	UserSettings::get()->setLogPath(lineEditLogPath->text());

	UserSettings::get()->setUseHttpProxy(checkBoxUseHttpProxy->isChecked());
	UserSettings::get()->setHttpProxy(lineEditHttpProxy->text());
	UserSettings::get()->setHttpProxyPort(lineEditPort->text().toInt());

	UserSettings::get()->setProxyUserName(lineEditProxyUserName->text());
	UserSettings::get()->setProxyPassword(lineEditProxyPassword->text());
	UserSettings::get()->setSaveProxyPassword(checkBoxSaveProxyPassword->isChecked());

	UserSettings::get()->save();
	QDialog::accept();
}

void SettingsDialog::slotUpdateHttpProxySettingsWidgets()
{
	bool useHttpProxy = checkBoxUseHttpProxy->isChecked();
	bool authenticationRequired = (useHttpProxy && checkBoxProxyAuthentication->isChecked());

	lineEditHttpProxy->setEnabled(useHttpProxy);
	lineEditPort->setEnabled(useHttpProxy);
	checkBoxProxyAuthentication->setEnabled(useHttpProxy);
	
	lineEditProxyUserName->setEnabled(authenticationRequired);
	lineEditProxyPassword->setEnabled(authenticationRequired);
	checkBoxSaveProxyPassword->setEnabled(authenticationRequired);
}

void SettingsDialog::slotBrowseLogPath()
{
	QString dir = QFileDialog::getExistingDirectory(
	                      this, tr("Log Directory"), lineEditLogPath->text(),
	                      QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks );
	
	if (!dir.isEmpty()) {
		lineEditLogPath->setText(dir);
	}
}

